﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using giaoxuhaison.Models;
using giaoxuhaison.Common;
using giaoxuhaison.MembershipServices;

namespace giaoxuhaison.Controllers
{
    public class HomeController : Controller
    {
        private gxhaisonEntities conn = new gxhaisonEntities();

        public ActionResult Index()
        {
            ViewBag.Message = "Modify this template to jump-start your ASP.NET MVC application.";
            ViewData["lstLabel"] = GetLabel();

            return View();
        }

        public ActionResult About()
        {
            ViewBag.Message = "Your app description page.";

            ViewData["lstAbout"] = GetAbout();
            return View();
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
        public ActionResult _FooterPage()
        {
            return View();
        }

        public ActionResult NewsBox()
        {
            ViewData["lstAll"] = GetNewAll();
            return PartialView();
        }
        public ActionResult LinkPath()
        {
            return PartialView();
        }

        /*--Định nghĩa các hàm lấy dữ liệu*/
        private List<About> GetAbout()
        {
            var lstGroup = conn.Abouts.Where(t => t.Locked == false).OrderByDescending(t => t.CreationDate).Take(1).ToList();
            return lstGroup;
        }

        private List<LabelAll> GetLabel()
        {
            var lstItem = conn.LabelAlls.Where(t => t.Locked == false).OrderByDescending(t => t.CreationDate).Take(1).ToList();
            return lstItem;
        }

        private List<vw_All_Index> GetNewAll()
        {
            var lstItem = conn.vw_All_Index.Where(t => t.Locked == false).OrderByDescending(t => t.CreationDate).ToList();
            return lstItem;
        }
       /*End*/

        [HttpPost]
        public ActionResult Contact(ContactModels model)
        {
            if (ModelState.IsValid)
            {
                try
                {
                    ContactService ContactSvr = new ContactService();
                    MembershipStatus createStatus = ContactSvr.Inserted(model);
                    ContactSvr = null;

                    if (createStatus != MembershipStatus.Success)
                        ModelState.AddModelError("", ValidateMessages.ErrorDescription(createStatus));
                    else
                        ModelState.AddModelError("", "Cảm ơn bạn đã gửi tin. Chúng tôi sẽ trả lời trong thời gian sớm nhất.");                    
                }
                catch
                {
                    ModelState.AddModelError("", ValidateMessages.ErrorDescription(MembershipStatus.Error));
                }
            }

            return View(model);
        }

        /*phan download tai lieu*/

    }
}
